package com.yc.sole.system.controller;

import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.ConfigBiz;
import com.yc.sole.system.request.ConfigReq;
import com.yc.sole.system.response.ConfigResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yizuomin
 * @date 2024-01-23
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ConfigController {

    private final ConfigBiz configBiz;

    /**
     * 初始化
     *
     * @return RestResult
     */
    @PreAuth(value = "system:config:save", description = "初始化系统配置", check = false)
    @PostMapping("/api/system/config/init")
    public Result<Void> init() {
        configBiz.init();
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param configReq
     * @return RestResult
     */
    @PreAuth(value = "system:config:update", description = "修改系统配置", check = false)
    @PostMapping("/api/system/config/updateConfigById")
    public Result<Void> updateConfigById(@RequestBody @Validated(UpdateGroup.class) ConfigReq configReq) {
        configBiz.updateConfigById(configReq);
        return Result.success();
    }

    /**
     * 查询
     *
     * @return Result
     */
    @PreAuth(value = "system:config:select", description = "查询系统配置", check = false)
    @PostMapping("/api/system/config/selectConfig")
    public Result<ConfigResp> selectConfig() {
        return Result.data(configBiz.selectConfig());
    }
}
